#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

def setup_environment_variables():
    """设置环境变量"""
    
    print("环境变量配置工具")
    print("=" * 50)
    
    # 获取用户输入
    print("请输入您的API密钥：")
    
    # 通义千问API密钥
    dashscope_key = input("请输入DASHSCOPE_API_KEY（通义千问API密钥）: ").strip()
    if not dashscope_key:
        print("⚠️  未输入DASHSCOPE_API_KEY，将使用默认值")
        dashscope_key = "your_dashscope_api_key"
    
    # 学历查询API密钥
    education_key = input("请输入MY_XXW_BGCX_API_KEY（学历查询API密钥）: ").strip()
    if not education_key:
        print("⚠️  未输入MY_XXW_BGCX_API_KEY，将使用默认值")
        education_key = "your_education_api_key"
    
    # 设置环境变量
    os.environ['DASHSCOPE_API_KEY'] = dashscope_key
    os.environ['MY_XXW_BGCX_API_KEY'] = education_key
    
    print("\n✅ 环境变量设置完成！")
    print(f"DASHSCOPE_API_KEY: {dashscope_key[:10]}..." if len(dashscope_key) > 10 else f"DASHSCOPE_API_KEY: {dashscope_key}")
    print(f"MY_XXW_BGCX_API_KEY: {education_key[:10]}..." if len(education_key) > 10 else f"MY_XXW_BGCX_API_KEY: {education_key}")
    
    return dashscope_key, education_key

def test_environment():
    """测试环境变量是否设置成功"""
    
    print("\n测试环境变量...")
    
    dashscope_key = os.getenv("DASHSCOPE_API_KEY")
    education_key = os.getenv("MY_XXW_BGCX_API_KEY")
    
    if dashscope_key and dashscope_key != "your_dashscope_api_key":
        print("✅ DASHSCOPE_API_KEY: 已设置")
    else:
        print("❌ DASHSCOPE_API_KEY: 未设置或使用默认值")
    
    if education_key and education_key != "your_education_api_key":
        print("✅ MY_XXW_BGCX_API_KEY: 已设置")
    else:
        print("❌ MY_XXW_BGCX_API_KEY: 未设置或使用默认值")
    
    return dashscope_key and education_key

def run_xueli_test():
    """运行xueli.py测试"""
    
    print("\n是否要运行xueli.py测试？(y/n): ", end="")
    choice = input().strip().lower()
    
    if choice == 'y' or choice == 'yes':
        try:
            print("\n正在运行xueli.py...")
            import subprocess
            result = subprocess.run([sys.executable, "ai_resume_generator/xueli.py"], 
                                  capture_output=True, text=True, timeout=30)
            
            print("输出结果:")
            print(result.stdout)
            
            if result.stderr:
                print("错误信息:")
                print(result.stderr)
                
        except subprocess.TimeoutExpired:
            print("❌ 运行超时")
        except Exception as e:
            print(f"❌ 运行失败: {e}")
    else:
        print("跳过测试")

if __name__ == "__main__":
    import sys
    
    # 设置环境变量
    setup_environment_variables()
    
    # 测试环境变量
    if test_environment():
        print("\n✅ 环境变量配置成功！")
        run_xueli_test()
    else:
        print("\n❌ 环境变量配置失败，请检查输入")
    
    print("\n" + "=" * 50)
    print("配置完成！")
